package com.chennan.javanetwork.bio;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @author chennan
 * @create 2021-07
 *
 * 数据结构
 */
public class BioServiceSocket {

    private ServerSocket serverSocket;

    private Executor executor;


    public BioServiceSocket(){
        this.init();
    }

    public void init(){
        executor = Executors.newFixedThreadPool(20);
        try {
            serverSocket=new ServerSocket(8080);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void accept(){
        while(true){
            try {
                Socket socket = serverSocket.accept();
                executor.execute(()->{
                    //优化 增加线程池进行异步处理
                    try {
                        InputStream inputStream = socket.getInputStream();
                        System.out.println("接受到了一个连接,数据是："+new BufferedReader(new InputStreamReader(inputStream)).readLine());
                        OutputStream outputStream = socket.getOutputStream();
                        //写出数据

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                });

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) {

    }

}

